package stark.app.function.view.photomodelview;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.app.R;
import stark.app.databinding.FragmentPhotoModelViewBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.FastClickUtil;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.RxUtil;

/**
 * Author：daizhongbin on 2021/7/29
 * Email：1027154338@qq.com
 * Desc: PhotoModelView使用demo界面
 */
public class PhotoModelViewFragment extends BaseNoModelFragment<FragmentPhotoModelViewBinding> {

    @Override
    protected int onCreate() {
        return R.layout.fragment_photo_model_view;
    }

    @Override
    protected void initView() {
        //layoutManager
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mDataBinding.rvModels.setLayoutManager(layoutManager);
        //item decoration
        mDataBinding.rvModels.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int right = SizeUtils.dp2px(15);
                outRect.set(0, 0, right, 0);
            }
        });

        //adapter
        PhotoModelViewAdapter viewAdapter = new PhotoModelViewAdapter();
        viewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                mDataBinding.photoModelView.getModelImgView().setImageResource(viewAdapter.getItem(position));
                viewAdapter.setSelPos(position);
            }
        });
        viewAdapter.setNewInstance(loadDatas());
        mDataBinding.rvModels.setAdapter(viewAdapter);

        ToastUtils.showLong("点击模板可替换底部图片");
        mDataBinding.photoModelView.setOnClickListener(v -> {
            selPic();
        });
        //默认为设置第一个模板
        mDataBinding.photoModelView.getModelImgView().setImageResource(loadDatas().get(0));

        //save
        mDataBinding.btnSave.setOnClickListener(v -> {
            clickSave();
        });

    }

    @Override
    protected void initData() {
    }

    private List<Integer> mDataList;

    private List<Integer> loadDatas() {
        if (mDataList != null) return mDataList;
        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.ic_pmv_model_1);
        list.add(R.drawable.ic_pmv_model_2);
        mDataList = list;
        return list;
    }

    private void selPic() {
        PhotoSelectorBuilder.builder(getActivity())
                .mode(PhotoConfig.Mode.PHOTO)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        if (data == null) return;
                        String path = data.get(0).getTargetPath();
                        Glide.with(getContext()).load(path).into(mDataBinding.photoModelView.getTfImgView());
                    }
                });
    }

    private void clickSave() {
        if (FastClickUtil.isFastClick()) return;
        RxUtil.create(new RxUtil.Callback<Uri>() {
            @Override
            public void doBackground(ObservableEmitter<Uri> emitter) {
                Bitmap bitmap = mDataBinding.photoModelView.getRetBitmap();
                Uri uri = FileP2pUtil.saveBitmap2Png(getContext(), bitmap);
                emitter.onNext(uri);
            }

            @Override
            public void accept(Uri uri) {
                if (uri != null) {
                    ToastUtils.showShort(com.stark.common.res.R.string.save_success);
                } else {
                    ToastUtils.showShort(com.stark.common.res.R.string.save_failure);
                }
            }
        });
    }
}
